package com.example.demo.controller;


import com.example.demo.entity.request.apiTestCaseStepReq.ApiTestCaseStepReq;
import com.example.demo.entity.request.apiTestCaseStepReq.UpdateApiTestCaseStepsReq;
import com.example.demo.entity.response.apiTestCaseStepResp.ApiTestCaseStepPageResp;
import com.example.demo.entity.ResponseInfo;
import com.example.demo.service.ApiTestCaseStepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/TestCaseStep")
@Api(tags = "测试用例步骤管理")
public class ApiTestCaseStepController {

    @Resource
    private ApiTestCaseStepService apiTestCaseStepService;

    /**
     * @param testCaseId 测试用例主键
     * @return 返回测试用例步骤集合，根据序号排序
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据用例id获取测试用例步骤")
    @ApiImplicitParam(name = "testCaseId", value = "测试用例id", required = true, dataType = "int")
    public ResponseInfo<List<ApiTestCaseStepPageResp>> findStepListByTestCaseId(@RequestParam int testCaseId) {
        return ResponseInfo.successInfo(apiTestCaseStepService.findStepListByTestCaseId(testCaseId));
    }

    /**
     * 更新用例执行步骤及环境参数
     * @param updateApiTestCaseStepReq 测试用例步骤集合
     * @return 返回结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新用例步骤")
    public ResponseInfo<String> updateStepInCase(@RequestBody UpdateApiTestCaseStepsReq updateApiTestCaseStepReq) {
        apiTestCaseStepService.apiTestCaseStepsEdit(updateApiTestCaseStepReq);
        return ResponseInfo.successInfo("");
    }

    /**
     * 更新单条步骤
     * @param apiTestCaseStepReq 测试步骤
     * @return 返回结果
     */
    @PostMapping("/updateOne")
    @ApiOperation(value = "更新用例步骤通过id")
    public ResponseInfo<String> updateStepById(@RequestBody ApiTestCaseStepReq apiTestCaseStepReq) {
        apiTestCaseStepService.updateStepById(apiTestCaseStepReq);
        return ResponseInfo.successInfo("");
    }

    /**
     * @param stepId 步骤id
     * @return 返回结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "刪除用例步驟")
    public ResponseInfo<String> deleteStepInCase(@PathVariable("id") int stepId) {
        if (stepId != 0) {
            apiTestCaseStepService.apiTestCaseStepDelete(stepId);
        }
        return ResponseInfo.successInfo("");
    }

    /**
     * @param ids 用例主键集合
     * @return 返回结果
     */
    @PostMapping("/deletes")
    @ApiOperation(value = "批量删除测试用例接口")
    @ApiImplicitParam(name = "ids", value = "id列表", required = true, dataType = "List")
    public ResponseInfo<String> deleteStepsInCase(@RequestParam List<Integer> ids) {
        if (ids.size() > 0) {
            apiTestCaseStepService.apiTestCaseStepsDelete(ids);
            return ResponseInfo.successInfo("");
        } else {
            return ResponseInfo.errorInfo("ids不能为空");
        }
    }

    /**
     * @param insertApiTestCaseStepReqs 测试用例步骤集合
     * @return 返回结果
     */
    @PostMapping("/addApiStep")
    @ApiOperation(value = "添加测试用例步骤接口")
    public ResponseInfo<String> addApiStep(@RequestBody List<ApiTestCaseStepReq> insertApiTestCaseStepReqs) {
        apiTestCaseStepService.insertStepToTestCase(insertApiTestCaseStepReqs);
        return ResponseInfo.successInfo("");
    }


}
